package com.example.supernote.ui.note.utils

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import androidx.activity.result.contract.ActivityResultContract
import androidx.core.content.FileProvider
import androidx.core.content.contentValuesOf
import java.io.File

/**
 * 上传照片
 */
class SelectPhotoContract: ActivityResultContract<Unit?, Uri?>() {
    override fun createIntent(context: Context, input: Unit?): Intent =
        Intent(Intent.ACTION_GET_CONTENT).setType("image/*")

    override fun parseResult(resultCode: Int, intent: Intent?): Uri? =
        intent?.data
}

/**
 * 拍照
 */
class TakePhotoContract(
    private var fileName: String = "IMG_${System.currentTimeMillis()}"
): ActivityResultContract<Unit?, Uri?>() {
    private var uri: Uri? = null

    override fun createIntent(context: Context, input: Unit?): Intent {
        uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            // Android 10 及以上获取图片uri
            context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValuesOf())
        } else {
            // Android 9 及以下获取图片uri
            FileProvider.getUriForFile(
                context, "${context.packageName}.provider",
                File(context.externalCacheDir, "IMG_${System.currentTimeMillis()}.jpg")
            )
        }
        return Intent(MediaStore.ACTION_IMAGE_CAPTURE).putExtra(MediaStore.EXTRA_OUTPUT, uri)
    }

    override fun parseResult(resultCode: Int, intent: Intent?): Uri? =
        if (resultCode == Activity.RESULT_OK) uri
        else null

    fun setFileName(name: String) {
        fileName = name
    }
}

/**
 * 上传音频
 */
class SelectAudioContract: ActivityResultContract<Unit?, Uri?>() {
    override fun createIntent(context: Context, input: Unit?): Intent =
        Intent(Intent.ACTION_GET_CONTENT).setType("audio/*")


    override fun parseResult(resultCode: Int, intent: Intent?): Uri? =
        intent?.data
}

/**
 * 上传视频
 */
class SelectVideoContract: ActivityResultContract<Unit?, Uri?>() {
    override fun createIntent(context: Context, input: Unit?): Intent =
        Intent(Intent.ACTION_GET_CONTENT).setType("video/*")

    override fun parseResult(resultCode: Int, intent: Intent?): Uri? =
        intent?.data
}


/**
 * 录屏
 */
class TakeVideoContract(
    private var fileName: String = "IMG_${System.currentTimeMillis()}"
): ActivityResultContract<Unit?, Uri?>() {
    private var uri: Uri? = null

    override fun createIntent(context: Context, input: Unit?): Intent {
        uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            // Android 10 及以上获取视频uri
            context.contentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValuesOf())
        } else {
            // Android 9 及以下获取视频uri
            FileProvider.getUriForFile(
                context, "${context.packageName}.provider",
                File(context.externalCacheDir, "/VIDEO_${System.currentTimeMillis()}.mp4")
            )
        }
        return Intent(MediaStore.ACTION_VIDEO_CAPTURE).putExtra(MediaStore.EXTRA_OUTPUT, uri)
    }

    override fun parseResult(resultCode: Int, intent: Intent?): Uri? =
        if (resultCode == Activity.RESULT_OK) uri
        else null

    fun setFileName(name: String) {
        fileName = name
    }
}

